package com.bkhech.home.practice.spring.el;

import com.bkhech.home.practice.spring.el.util.StringUtils;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

/**
 * EL 函数引用
 *
 * You can extend SpEL by registering user-defined functions that
 * can be called within the expression string.
 * The function is registered through the EvaluationContext.
 *
 * 注意：解析时，引用方法前缀 #
 *
 * @author guowm
 * @date 2023/1/29
 */
public class ElFunctionsDemo {
    public static void main(String[] args) throws NoSuchMethodException {
        ExpressionParser parser = new SpelExpressionParser();

        EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
        context.setVariable("reverseString", StringUtils.class.getDeclaredMethod("reverseString", String.class));

        final String value = parser.parseExpression("#reverseString('hello')").getValue(context, String.class);
        System.out.println(value);

    }
}
